package cn.itcast.goods.user.service;

import java.io.IOException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;

import cn.itcast.goods.user.dao.UserDao;
import cn.itcast.goods.user.domain.User;
import cn.itcast.goods.user.service.exception.UserException;
import cn.mytools.commons.CommonUtils;
import cn.mytools.mail.Mail;
import cn.mytools.mail.MailUtils;

/**
 * 用户模块业务层
 * 
 * @author HL
 *
 */
public class UserService {
	private UserDao userDao = new UserDao();

	/**
	 * ajax 用户名校验
	 * 
	 * @param loginname
	 * @return
	 */
	public boolean ajaxValidateLoginname(String loginname) {
		try {
			return userDao.ajaxValidateLoginname(loginname);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public boolean ajaxValidateEmail(String email) {
		try {
			return userDao.ajaxValidateEmail(email);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 用户注册功能
	 * 
	 * @param user
	 */
	public void regist(User user) {

		user.setUid(CommonUtils.uuid());
		user.setStatus(0);
		user.setActivationCode(CommonUtils.uuid() + CommonUtils.uuid());

		try {
			userDao.add(user);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		/*
		 * 发邮件
		 */
		// 加载配置文件
		Properties prop = new Properties();

		try {
			prop.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		String host = prop.getProperty("host");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		Session session = MailUtils.createSession(host, username, password);

		String from = prop.getProperty("from");
		String to = user.getEmail();
		String subject = prop.getProperty("subject");
		// MessageFormat.format 会把第一个参数中的{0}占位符，使用第二个参数来替换。
		// 例如 MessageFormat.format(你好{0}，你{1}，张三，去屎吧); 返回你好张三，你去屎把
		String content = MessageFormat.format(prop.getProperty("content"), user.getActivationCode());
		Mail mail = new Mail(from, to, subject, content);
		try {
			MailUtils.send(session, mail);
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 激活功能
	 * 
	 * @param activationCode
	 * @throws UserException
	 */
	public void activation(String activationCode) throws UserException {
		try {
			User user = userDao.findByActivationCode(activationCode);
			if (user == null)
				throw new UserException("无效的激活码！");
			if (user.getStatus() == 1)
				throw new UserException("已经激活过了，不要二次激活！");
			userDao.updateStatus(user.getUid(), 1);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

	public User login(User user) {

		try {
			return userDao.findByUserNameAndPassWord(user.getLoginname(), user.getLoginpass());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void updatePassword(User user) throws UserException {
		try {
			if (userDao.findByUserNameAndPassWord(user.getLoginname(), user.getLoginpass()) == null) {
				throw new UserException("原密码错误！");
			} else {
				userDao.updatePassword(user.getLoginname(), user.getNewpass());
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public User findByUid(String uid) {
		try {
			return userDao.findByUid(uid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public boolean findByLoginnameAndEmail(String loginname, String email) {
		try {
			return userDao.findByLoginnameAndEmail(loginname, email);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public String sendEmailVerify(User user) {
		/*
		 * 发邮件
		 */
		// 加载配置文件
		Properties prop = new Properties();

		try {
			prop.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		String host = prop.getProperty("host");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		Session session = MailUtils.createSession(host, username, password);

		String from = prop.getProperty("from");
		String to = user.getEmail();
		String subject = "来自网上书城的邮件";	
		String emailVerify = CommonUtils.uuid().substring(0,6);
		String content = "请复制此验证码完成验证      "+emailVerify;
		
		Mail mail = new Mail(from, to, subject, content);
		try {
			MailUtils.send(session, mail);
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return emailVerify;
	}
}
